﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


namespace Engine.Dtd.Schema {
    /// <summary>
    /// Provides a simple and flexible method of encoding and naming arbitrary parts
    /// of the actual content of DTD schema in a portable way.
    /// </summary>
    [DebuggerDisplay("{Name}")]
    public class DEntity {
        /// <summary>
        /// Checks whether a token denotes a literal entity or not.
        /// </summary>
        /// <param name="token">The token to check.</param>
        /// <param name="result">When this method returns, contains the literal type equivalent to the <paramref name="token"/> parameter.</param>
        /// <returns>true if the token is <see cref="LiteralType"/>, otherwise false.</returns>
        public static bool TryParseLiteralType(string token, out LiteralType result) {
            Guard.ArgumentNotNull(token, "token");
            switch (token) {
                case "CDATA":
                    result = LiteralType.CData;
                    return true;
                case "SDATA":
                    result = LiteralType.SData;
                    return true;
                case "PI":
                    result = LiteralType.Pi;
                    return true;
                default:
                    result = LiteralType.CData;
                    return false;
                }
            }

        /// <summary>
        /// Sets the entity to be a literal of the type specified.
        /// </summary>
        /// <param name="token">One of <see cref="LiteralType"/> enumerator value.</param>
        /// <returns>A LiteralType instance.</returns>
        public static LiteralType ParseLiteralType(string token) {
            Guard.ArgumentNotNull(token, "token");
            LiteralType literalType;
            if (TryParseLiteralType(token, out literalType))
                return literalType;
            else
                throw new DtdException("The specified token could not recognized as a LiteralType instance.");
            }

        /// <summary>
        /// Gets the value indicating whether this entity is an external entity.
        /// </summary>
        public bool External { get; internal set; }
        /// <summary>
        /// Gets the type of literal of this entity.
        /// </summary>
        public LiteralType LiteralType { get; internal set; }
        /// <summary>
        /// Gets the literal value of this entity.
        /// </summary>
        public string Literal { get; internal set; }
        /// <summary>
        /// Gets the name of this entity.
        /// </summary>
        public string Name { get; internal set; }
        }
    }
